package xin.clips.web.controller.gy;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import xin.clips.common.core.domain.AjaxResult;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 统计分析
 *
 * @author clips
 * @date 2023-01-12
 */
@Controller
@RequestMapping("/gy/statistics")
public class StatisticsController {

    private final String prefix = "gy/statistics";

    @RequiresPermissions("gy:statistics:view")
    @RequestMapping("/device")
    public String userAdd(ModelMap mmap) {
        int allNum = 0; //总设备数

        int dmjqrNum = 0; //打磨机器人
        int jxbNum = 0; //机械臂

        int xjjqrNum = 0; //巡检机器人
        int cyjcNum = 0; //冲压机床
        int skjcNum = 0; //数控机床

        mmap.put("allNum", "100");

        /* 设备总类统计*/
        mmap.put("dmjqrNum", "40");
        mmap.put("jxbNum", "30");
        mmap.put("xjjqrNum", "20");
        mmap.put("cyjcNum", "10");
        mmap.put("skjcNum", "6");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
//获得本月第一天
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        String startTime = sdf.format(calendar.getTime());

//获得本月最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        String endTime = sdf.format(calendar.getTime());
        mmap.put("startTime",startTime);
        mmap.put("endTime",endTime);
        return prefix + "/device";
    }

    @RequiresPermissions("gy:time:view")
    @RequestMapping("/deviceRun")
    @ResponseBody
    public AjaxResult search(String startTime, String endTime) {
        List<String> dateList = new ArrayList<>();
        Map<String, String> dateMap1 = new HashMap<>();
        Map<String, String> dateMap2 = new HashMap<>();
        dateList.add(startTime);
        dateMap1.put(startTime, "0");
        dateMap2.put(startTime, "0");
        String date = startTime;
        do {
            date = LocalDate.parse(date).plusDays(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            dateList.add(date);
            dateMap1.put(date, "0");
            dateMap2.put(date, "0");
        } while (!endTime.equals(date));


//
//        ArrayList<String> jqr1List = new ArrayList<>();
//        for (String date1 : dateList) {
//            jqr1List.add(dateMap2.get(date1));
//        }
        ArrayList<String> jq1List = new ArrayList<>();
        for (String jq1 : dateList) {
            int time = (int) (Math.random() * 100);
            jq1List.add(String.valueOf(time));
        }

        //机器人2
        ArrayList<String> jq2List = new ArrayList<>();
        for (String jq1 : dateList) {
            int time = (int) (Math.random() * 100);
            jq2List.add(String.valueOf(time));
        }


        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.put("dateArray", dateList);
        ajaxResult.put("jq1Array", jq1List);
        ajaxResult.put("jq2Array", jq2List);
        return ajaxResult;
    }
}
